package com.yc.navy.goods.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yc.navy.goods.bean.CollectionInfo;
import com.yc.navy.goods.dto.ConstantInfoDTO;
import com.yc.navy.goods.dto.MemberInfoDTO;
import com.yc.navy.goods.enums.ResultEnum;
import com.yc.navy.goods.service.ICollectionInfoService;
import com.yc.navy.goods.util.StringUtil;
import com.yc.navy.goods.vo.ResultVO;

@RestController
@RequestMapping("/collection")
public class CollectionInfoController {
	@Autowired
	private ICollectionInfoService collectionInfoSerive;
	
	private String getlogin(HttpSession session) {
		Object obj = session.getAttribute(ConstantInfoDTO.LOGINCHECK);
		if(obj == null) {
			return null;
		}
		MemberInfoDTO m = (MemberInfoDTO) obj;
		return m.getMid()+"";
	}
	
	@RequestMapping("/addCollection")
	public ResultVO addCollection(CollectionInfo collection,HttpSession session) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		
		collection.setMid(Integer.parseInt(mid));
		int result = collectionInfoSerive.addCollection(collection);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS);
	}
	
	@RequestMapping("/findCollection")
	public ResultVO findCollection(HttpSession session) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		List <CollectionInfo> list = collectionInfoSerive.findCollection(Integer.parseInt(mid));
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.NOT_DATA);
		}
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	
	@RequestMapping("/checkCollection")
	public ResultVO checkCollection(HttpSession session,Integer pid) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		CollectionInfo collection = new CollectionInfo();
		collection.setPid(pid);
		collection.setMid(Integer.parseInt(mid));
		CollectionInfo result = collectionInfoSerive.checkCollection(collection);
		if(result == null) {
			return new ResultVO(ResultEnum.SUCCESS);
		}
		
		return new ResultVO(ResultEnum.ERROR);
	}
	
	@RequestMapping("/deleteCollection")
	public ResultVO deleteCollection(HttpSession session,Integer ccid) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		
		if(StringUtil.checkNull(String.valueOf(ccid))) {
			return new ResultVO(ResultEnum.INCOMPLETEINFORMATION);
		}
		Integer result = collectionInfoSerive.deleteCollection(ccid);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		return new ResultVO(ResultEnum.SUCCESS);
	}
}
